<template>
  <el-dialog title="添加信息" :visible.sync="tempDialogVisible" width="550px" :close-on-click-modal="false" @close="close">
    <el-form status-icon :model="tempData" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="电话：" prop="phone">
        <el-input size="medium" v-model="tempData.phone"></el-input>
      </el-form-item>
      <el-form-item label="商标：">
        <el-input size="medium" v-model="tempData.brand"></el-input>
      </el-form-item>

      <el-form-item label="来源类型：">
        <el-select v-model="tempData.source_type" placeholder="请选择">
          <el-option
              v-for="item in source_type"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import tree from 'vue-giant-tree'
import { add } from '@/api/info'

export default {
  name: 'InfoEdit',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    sourceType: {
      type: Array,
      default: []
    },
    state: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      tempDialogVisible: false,
      tempData: {
        phone: '',
        brand: '',
        source_type: 1,
        state: 0,
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      },
      source_type: []
    }
  },
  watch: {
    dialogVisible: function(val) {
      this.tempDialogVisible = val
    },
    sourceType: function(val) {
      this.source_type = []
      val.forEach(v => {
        if (v.value != '' && v.value != 0) {
          console.log(v)
          this.source_type.push(v)
        }
      })
    }
  },
  mounted() {

  },
  methods: {
    selectDefault() {

    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          add(this.tempData).then(res => {
            this.$message.success('添加成功')
            this.close()
          })
        } else {
          return false
        }
      })
    },
    close() {
      this.$refs['ruleForm'].clearValidate()
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
